using System;
using System.Web;
using System.Web.UI;
using PetShop.Web.Controls;
using PetShop.Web.Entities;
using PetShop.Web.WebComponents;

namespace PetShop.Web
{
    public partial class OrderBilling : Page
    {
        protected AddressUI billAddr;
        protected NavBar header;
        protected StaticAddress staticAddressBilling;
        protected StaticAddress staticAddressShipping;


        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                enterAddress.Visible = true;
                confirmAddress.Visible = false;


                var myAccount = (AccountInfo) HttpContext.Current.Session["ACCOUNT_KEY"];

                if (myAccount == null)
                {
                    HttpContext.Current.Response.Redirect("WebFormSignIn.aspx", true);
                }


                if (myAccount != null && myAccount.Address != null)
                {
                    billAddr.Address = myAccount.Address;
                }
                else
                {
                    billAddr.Address = new AddressInfo();
                }
            }
        }


        private void InitializeComponent()
        {
        }


        protected void ContinueClicked(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {
                string cardType = CleanString.InputText(listCardType.SelectedItem.Text, 10);
                string cardNumber = CleanString.InputText(txtCardNumber.Text, 20);
                ;
                string cardYear = CleanString.InputText(listYear.SelectedItem.Text, 4);
                ;
                string cardMonth = CleanString.InputText(listMonth.SelectedItem.Text, 2);
                ;

                var creditCard = new CreditCardInfo(cardType, cardNumber, string.Format("{0}/{1}", cardMonth, cardYear));

                HttpContext.Current.Session["CREDITCARD_KEY"] = creditCard;

                AddressInfo billingAddress = billAddr.Address;


                HttpContext.Current.Session["BILLING_KEY"] = billingAddress;


                if (chkShipBilling.Checked)
                {
                    HttpContext.Current.Session["SHIPPING_KEY"] = HttpContext.Current.Session["BILLING_KEY"];
                }
                else
                {
                    HttpContext.Current.Response.Redirect("OrderShipping.aspx", true);
                }

                enterAddress.Visible = false;
                confirmAddress.Visible = true;

                staticAddressBilling.ShowAddress(billingAddress);
                staticAddressShipping.ShowAddress(billingAddress);
            }
        }
    }
}